var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var valide = libs.formValide;
var VTabs = require('modules/list/tabs_list');
var _user;

//当前页公共变量
var _page = {};
var _postdata = {};
var _list_container = false;
var _getType='';
// 测试hook
var menus = [
    {id: '', title: "全部订单"},
    {id: 'EXPRESS', title: "快递订单"},
    // {id: 'EXPRESS_SEND', title: "寄件订单"},
    {id: 'BUY_SKU', title: "送水订单"},
    {id: 'BUY_CARD', title: "卡劵订单"}
]
var lists = []

var menu_funs = {
    item: function(){
        $(this).click(function(){
            // 切换标签时清除滚动加载的数据
            if (_list_container){
                $(_list_container).scrollTop(0)
            }
            $(this).addClass('active').siblings().removeClass('active')
            var getOrdertype = $(this).attr('data-id');
            _page.pagenum = '';
            _page.gettype = getOrdertype;
            _getType = getOrdertype;
            lists = []
            setTimeout(function(){
                pullData(getOrdertype, function(){
                    SA.setter('ordersLists',{data: lists})
                })
            },500)
        })
    }
}

var list_funs = {
    item: function(){
        $(this).click(function(){
            var getOrderid= $(this).find('.order-li').attr('data-val');
            var getOrdertitle = $(this).find('.li_top em').html();
            var getcardurl = $(this).find('.li_top').attr('data-url');
            if(getOrderid){
                if(getOrdertitle === '寄件'){
                    // sessionStorage.setItem('sendOrderId',JSON.stringify({orderId: getOrderid}));
                    console.log(getOrderid);
                    router('send/details', {back: getOrderid})
                }else if(getOrdertitle === '收件'){
                    console.log(getOrderid);
                    router('expFinish', {back: getOrderid})
                }else if(getOrdertitle === '卡券'){
                    localStorage.setItem('savehistory',JSON.stringify(SA.get('_HISTORY')))
                    router('http://' + _user.info.domainName + '/' + getcardurl)

                }else if(getOrdertitle === '送水'){
                    router('water/item', {back: getOrderid})
                }
            }

        })
    },
    scrollEnd: function(_name, _next){
        if (!_list_container){
            _list_container = this;
        }
        _page.pagenum++;
        pullData('scroll',function(){
            _next(_name, lists)
        })
    }
}

var OrderHtml = React.createClass({
    render: function(){
        var items = this.props.data
        return(
            <div className='order-li' data-val={items.orderId}>
                <div className='li_top' data-url={items.orderUrl}><em>{items.title}</em></div>
                <div className='li_main'>
                    <em>服务金额<i>￥{items.price}</i></em>
                    <p>{items.orderTime.toString().substring(0,16)}</p>
                    <div className='li_position'>
                        <p>{items.statusDesc}<i className="iconfont icon-xiangyou"></i></p>
                    </div>
                </div>
            </div>
        )
    }
})


//拉取最新数据
function pullData(type, pagenum, cb){
    var _type
    if (type==='scroll'){
        _type = type;
        type = false
    }
    if (typeof type === 'function'){
        cb = type;
        type = false;
    }
    if (typeof pagenum === 'function'){
        cb = pagenum;
        pagenum = false;
    }
    var _pagenum = pagenum||_page.pagenum||1
    var postdata = {
        orderType: type||_page.gettype||_getType||'',
        pageNum: _pagenum++,
        login: _user.login,
        method: 'get'
    }
    api.req('/myOrderList', postdata, function(data){
        if (data.recordList){
            _page = {
                data: data,
                pagenum: data.currentPage
            }
            dealWithData(_type)
            if (cb){
                cb()
            }
        }
    })
}

//根据数据处理结构
function dealWithData(type){
    var _data = _page.data.recordList;

    lists = []
    if (_data && _data.length){
        _data.map(function(item){
            lists.push(
                <OrderHtml data={item}/>
            )
        })
    }
    else {
        if (type!=='scroll'){
            lists.push(
                <div className="water_no_card order_no_list">
                    <div className="water_no_card_com">
                        <i></i><p>您暂无订单，请先去下单吧</p>
                    </div>
                </div>
            )
        }
    }
}

function renderHtml(name){
    pullData(function(){
        VTabs(menus, lists, {
            container: name,
            globalName: {
                menu: 'ordersMenus',
                list: 'ordersLists'
            },
            itemMethod: {
                menu: menu_funs.item,
                list: list_funs.item
            },
            scrollEnd: {
                list: list_funs.scrollEnd
            },
            empty: {
                list: <div className="water_no_card order_no_list">
                    <div className="water_no_card_com">
                        <i></i><p>您暂无订单，请先去下单吧</p>
                    </div>
                </div>
            }
        })
        orderbind()
    })
}
function orderbind(){
    $('#uc_order .tabs-menu ul li:last-child').remove();
    $('#uc_order .tabs-menu ul li').eq(0).addClass('active');
}

function start(name){
    return pages.new({
        boot: function(){
            // pullData()
        },
        goback: function(){
            var _pre = router.pre();
            if(_pre){
                if (_pre.hash === 'login'){
                    router.cb = function(){
                        if (wx){
                            wx.closeWindow()
                        }
                    }
                }
                else{
                    router.cb = function(){
                        router.goback()
                    }
                }
            }

        },
        trigger:function(){
            this.libs.changeTitle('我的订单');    //更改当前页面标题
            this.main()
            if(localStorage.getItem('savehistory')){
                SA.set('_HISTORY',JSON.parse(localStorage.getItem('savehistory')))
                localStorage.removeItem('savehistory')
            }
        },
        ready: function(){},
        main: function(){
            var getOrdertype = $('#uc_order .tabs-menu ul li.active').attr('data-id');
            _page.pagenum = '';
            _page.gettype = getOrdertype;
            lists = [];

            function initData(){
                pullData(getOrdertype, function(){
                    SA.setter('ordersLists',{data: lists})
                    renderHtml(name);
                })
            }

            _user = SA.get('USER')
            if (_user.login){
                initData()
            }
            else {
                SA.set('USER', initData)
            }
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
